
package com.insigma.business.qrcode.soldier.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.soldier.dao.PlacementWordConfigDao;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.PlacementWordConfigDO;
import com.insigma.business.qrcode.soldier.request.PlacementWordConfigBSearcher;
import com.insigma.business.qrcode.soldier.service.PlacementWordConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service("placementWordConfigService")
public class PlacementWordConfigServiceImpl implements PlacementWordConfigService {

    @Autowired(required = false)
    private PlacementWordConfigDao placementWordConfigDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<PlacementWordConfigDO> search(final PlacementWordConfigBSearcher placementWordConfigBSearcher) {
        return placementWordConfigDao.search(placementWordConfigBSearcher);
    }
   
    @Override
    public PlacementWordConfigDO get(final String id){
        return placementWordConfigDao.get(id);
    }

    
    @Override
    public Integer insert(final PlacementWordConfigDO placementWordConfigDO){
        placementWordConfigDO.setId(sequenceGenerator.genKey(SequenceNameConstant.PLACEMENT_WORD_CONFIG_ID));
        return placementWordConfigDao.insert(placementWordConfigDO);
    }

    
    @Override
    public Integer update(final PlacementWordConfigDO placementWordConfigDO){
        return placementWordConfigDao.update(placementWordConfigDO);
    }

    @Override
    public LinkedHashMap filterByType(JSONObject parseObject, String type, Set<String> set) {
        if (parseObject == null) {
            return null;
        }

        List<PlacementWordConfigDO> placementWordConfigDOS = placementWordConfigDao.listByTypeAndAuth(type, Lists.newArrayList(set));
        List<String> codeSet = placementWordConfigDOS.stream().sorted(new Comparator<PlacementWordConfigDO>() {
            @Override
            public int compare(PlacementWordConfigDO o1, PlacementWordConfigDO o2) {
                return o1.getSeqNo().compareTo(o2.getSeqNo());
            }
        }).map(s -> s.getCode()).collect(Collectors.toList());

        LinkedHashMap result = new LinkedHashMap();
        if (!CollectionUtils.isEmpty(placementWordConfigDOS)) {
            for (String key : codeSet) {
                Object o = parseObject.get(key);
                result.put(key, o);
            }
        }
        return result;
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return placementWordConfigDao.delete(id,modifierType,modifierId);
    }

}
